package cn.ulc.socket.admin.service.marketing.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.advertising.AdvertisingDTO;
import cn.ulc.socket.admin.model.dto.advertising.AdvertisingListDTO;
import cn.ulc.socket.admin.model.param.advertising.AdvertisingParam;
import cn.ulc.socket.admin.service.marketing.AdvertisingServ;
import cn.ulc.store.dao.AdvertisingDao;
import cn.ulc.store.entity.Advertising;
import cn.ulc.store.entity.Company;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * 广告
 */
@Service
public class AdvertisingServImpl extends BaseService implements AdvertisingServ {
    private static final Log log = LogFactory.get();

    @Autowired
    AdvertisingDao advertisingDao;

    @Override
    public AdvertisingListDTO findList(String key,String startDate,String endDate, PaginationParam pager) {
        Company company = getCurrentCompany();
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",key);
        condition.put("startDate",startDate);
        condition.put("endDate",endDate);
        condition.put("companyId",company.getId());
        PageQuery pageQuery = initPageQuery(pager);
        pageQuery.setParas(condition);
        advertisingDao.findListPage(pageQuery);
        return new AdvertisingListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public AdvertisingDTO findDetail(Long id) {
        long aid = Convert.toLong(id,0l);
        Advertising entity = advertisingDao.findOne(aid);
        return new AdvertisingDTO(entity);
    }

    @Override
    public Long saveOne(AdvertisingParam param) {
        boolean isEdit = param.getId() != null && param.getId() > 0;
        Advertising entity = new Advertising();
        if(isEdit){
            entity = advertisingDao.findOne(param.getId());
            if(entity == null) throw new PlatformException("未获取到广告位信息");
        }
        setBaseInfo(entity);
        entity.setTitle(param.getTitle());
        entity.setImageUrl(param.getImageUrl());
        entity.setLinkUrl(param.getLinkUrl());
        entity.setLocation(param.getLocation());
        entity.setEndDate(param.getEndDate());
        entity.setStatus(AvailableStatus.AVAILABLE);
        advertisingDao.save(entity);
        //其他关联处理
        return entity.getId();
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        long aid = Convert.toLong(id,0l);
        Advertising entity = advertisingDao.findOne(aid);
        if(entity == null) throw new PlatformException("未获取到广告位信息");
        entity.setStatus(status);
        setBaseInfo(entity);
        advertisingDao.updateTemplateById(entity);
        return true;
    }

    @Override
    public Boolean deleteOne(Long id) {
        long aid = Convert.toLong(id,0l);
        Advertising entity = advertisingDao.findOne(aid);
        if(entity != null) {
            entity.setDeleted(DeletedStatus.DELETED);
            setBaseInfo(entity);
            advertisingDao.updateTemplateById(entity);
        }
        return true;
    }
}
